package com.dangs.autotest.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dangs.autotest.R;
import com.dangs.autotest.test.BaseTask;
import com.dangs.autotest.test.BaseTest;
import com.dangs.autotest.test.Task;
import com.dangs.autotest.tool.L;

import java.util.List;

public class CheckListViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "CheckListViewAdapter";
    private List<Task> list;
    private final int TYPE_UNKNOWN = 0;
    private final int TYPE_TASK = 1;
    private final int TYPE_TEST = 2;

    public CheckListViewAdapter(List<Task> list) {
        this.list = list;
    }

    public CheckListViewAdapter updateList(List<Task> list) {
        this.list = list;
        return this;
    }

    @Override
    public int getItemViewType(int position) {
        Task task = list.get(position);
        if (task instanceof BaseTest) {
            return TYPE_TEST;
        } else if (task instanceof BaseTask) {
            return TYPE_TASK;
        }
        return TYPE_UNKNOWN;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CheckDataHolder(LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_check_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Task task = list.get(position);
        task.setId(position + 1);
        holder.itemView.setOnClickListener(v -> onItemClick(v, position));
        ((CheckDataHolder) holder).name.setText("" + task.getId() + "." + task.getName());
        ((CheckDataHolder) holder).checkBox.setChecked(task.isChecked());
        ((CheckDataHolder) holder).checkBox.setOnCheckedChangeListener((box, checked) -> {
            onTaskChanged(position, checked);
        });
    }

    private void onTaskChanged(int position, boolean checked) {
        int type = getItemViewType(position);
        Task task = list.get(position);
        L.d(TAG, "position:" + position + "->" + checked + ", task:" + task);
        task.check(checked);
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    private void onItemClick(View v, int position) {
        if (getItemViewType(position) == TYPE_TASK) {
            Task task = list.get(position);
            L.d(TAG, "onItemClick:" + position + ",test:" + task);
            task.check(!task.isChecked());
        }
    }

    class CheckDataHolder extends RecyclerView.ViewHolder {
        TextView name;
        CheckBox checkBox;

        public CheckDataHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tx_info);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }

}
